<?php

namespace app\proxy\controller\Product;

use app\proxy\controller\Controller;
use app\proxy\model\product\Category as CategoryModel;
use app\proxy\model\auth\User as ProxyUser;

/**
 * 商品分类
 */
class Category extends Controller
{
    /**
     * 商品分类列表
     */
    public function index()
    {
        $model   = new CategoryModel;
        $param   = $this->postData();
        $regions = $this->getRegionIds();
        $list    = $model->getProxyCategoryTree($param, $regions, $this->getProxyId());
        // 数量
        $category_count = [
            'using'   => $model->getProxyTotal(['audit_status' => 1], $regions),
            'audit'   => $model->getProxyTotal(['audit_status' => 0], $regions),
            'no_pass' => $model->getProxyTotal(['audit_status' => 2], $regions),
        ];
        return $this->renderSuccess('', compact('list', 'category_count'));
    }

    // /**
    //  * 商品分类列表
    //  */
    // public function index()
    // {
    //     $model = new CategoryModel;
    //     $param = $this->postData();
    //     $list  = $model->getCacheTree(2, $param);
    //     // 数量
    //     $proxyCate = new \app\proxy\model\product\ProxyCategory();
    //     $category_count = [
    //         'using'   => $proxyCate->getTotalByWhere(['audit_status' => 1]),
    //         'audit'   => $proxyCate->getTotalByWhere(['audit_status' => 0]),
    //         'no_pass' => $proxyCate->getTotalByWhere(['audit_status' => 2]),
    //     ];
    //     return $this->renderSuccess('', compact('list', 'category_count'));
    // }

    /**
     * 添加商品分类
     */
    public function add()
    {
        $nosh = ProxyUser::checkauth($this->getProxyId(),'category');
        $model = new CategoryModel;
        // 新增记录
        if ($model->add(array_merge($this->postData(), $nosh?['audit_status' => 1]:['audit_status' => 0]), $this->getProxyId())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑商品分类
     */
    public function edit($category_id)
    {
        $nosh = ProxyUser::checkauth($this->getProxyId(),'category');
        // 模板详情
        $model = CategoryModel::detail($category_id);
        if ($model->isEmpty() || $model['proxy_id'] != $this->getProxyId()) {
            return $this->renderError('商品分类不存在');
        }
        // 更新记录
        if ($model->edit(array_merge($this->request->post(), $nosh?['audit_status' => 1]:['audit_status' => 0]), $this->getProxyId())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除商品分类
     */
    public function delete($category_id)
    {
        $model = (new \app\shop\model\product\Category())->find($category_id);
        if ($model->isEmpty() || $model['proxy_id'] != $this->getProxyId()) {
            return $this->renderError('商品分类不存在');
        }
        if ($model->remove($category_id)) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '删除失败');
    }

    /**
     * 得到修改图片
     * @return array
     */
    public function image($category_id)
    {
        $model  = new CategoryModel;
        $detail = $model->detailWithImage(['category_id' => $category_id]);
        return $this->renderSuccess('', compact('detail'));
    }

}
